package com.luntan.school.dao.impl;

import com.luntan.school.dao.TagDao;
import com.luntan.school.model.Tag;
import com.luntan.school.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class TagDaolmpl implements TagDao {
    @Override
    public List<Tag> getAllTags() {
        try {
            Connection connection = DBUtil.getConnection();

            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery("select * from tag");

            //以封装结果为列表数据
            List<Tag> tags = new ArrayList<>();
            while (resultSet.next()) {
                Tag tag = new Tag();

                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                tag.setId(id);
                tag.setName(name);
                tags.add(tag);
            }
            connection.close();
            statement.close();
            resultSet.close();

            return tags;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Set<Tag> getTagsByPostsId(int postsId) {
        try {
            Connection connection = DBUtil.getConnection();

            PreparedStatement preparedStatement = connection.prepareStatement("select * from tag,posts_tag where tag.id=posts_tag.tag_id and posts_tag.posts_id=?");
            preparedStatement.setInt(1,postsId);
            ResultSet resultSet = preparedStatement.executeQuery();

            Set<Tag> tags = new HashSet<>();
            while (resultSet.next()) {
                Tag tag = new Tag();

                int id = resultSet.getInt("tag_id");
                String name = resultSet.getString("name");
                tag.setId(id);
                tag.setName(name);
                tags.add(tag);
            }
            preparedStatement.close();
            connection.close();
            resultSet.close();

            return tags;
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean isExist(Integer postsId, Integer tagId) {
        try {
            Connection connection = DBUtil.getConnection();

            PreparedStatement preparedStatement = connection.prepareStatement("select * from posts_tag where posts_id=? and tag_id=?");
            preparedStatement.setInt(1, postsId);
            preparedStatement.setInt(2, tagId);
            ResultSet resultSet = preparedStatement.executeQuery();
            boolean result = resultSet.next();

            resultSet.close();
            preparedStatement.close();
            connection.close();

            return result;
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public void addPostsTag(Integer postsId, Integer tagId) {

        try {
            Connection connection = DBUtil.getConnection();

            PreparedStatement preparedStatement = connection.prepareStatement("insert into posts_tag(posts_id, tag_id) values(?, ?)");
            preparedStatement.setInt(1, postsId);
            preparedStatement.setInt(2, tagId);
            preparedStatement.executeUpdate();

            preparedStatement.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void deletePostsTags(Integer postsId) {

        try {
            Connection connection = DBUtil.getConnection();

            PreparedStatement preparedStatement = connection.prepareStatement("delete from posts_tag where posts_id = ?");
            preparedStatement.setInt(1, postsId);
            preparedStatement.executeUpdate();

            preparedStatement.close();
            connection.close();
        }catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        TagDao tagDao = new TagDaolmpl();

//        tagDao.addPostsTag(562302,6712);
        System.out.println(tagDao.getTagsByPostsId(562302));
    }
}
